package com.example.selfdefinescope.config;

import com.example.selfdefinescope.CustomScope;
import com.example.selfdefinescope.bean.Car;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.Collections;

@Configuration
public class AppConfig {

    @Bean
    @Scope(scopeName = CustomScope.CUSTOM_SCOPE)
    public Car car(){
        return new Car("东方红");
    }

    // 将自定义作用域配置到Spring容器中
    @Bean
    public CustomScopeConfigurer customScopeConfigurer(){
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();
        configurer.setScopes(Collections.singletonMap(CustomScope.CUSTOM_SCOPE, new CustomScope()));
        return configurer;
    }
}
